package com.ita.os.merchant.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
import org.junit.Test;

import com.ita.os.merchant.po.Merchant;


public class XmlResolverUtil {

	private static ConnectionFactory factory;
	private static Destination queue;

	public static void init() throws FileNotFoundException, IOException {
		Properties props = new Properties();
		InputStream in = null;
		Connection con = null;
		in = XmlResolverUtil.class.getClassLoader().getResourceAsStream("jms_config.properties");
		ConnectionFactory f = null;
		Destination q = null;
		try {
			props.load(in);
			f = new ActiveMQConnectionFactory(props.getProperty("url"));
			q = new ActiveMQQueue(props.getProperty("queue"));
		} catch (IOException e) {
			e.printStackTrace();
		}
		factory = f;
		queue = q;
	}
	
	public static String merchantToXML(Merchant m) throws IOException,
			JMSException {
		Document doc = new Document();
		Element root = new Element(m.getClass().getSimpleName());
		doc.setRootElement(root);
		
		Element mname = new Element("mname");
		mname.setText(m.getMname());

		Element type = new Element("type");
		type.setText(m.getType());

		Element addr = new Element("address");
		addr.setText(m.getAddress());

		Element tel = new Element("tel");
		tel.setText(m.getTel());

		Element logo = new Element("logo");
		logo.setText(m.getLogo());

		Element info = new Element("info");
		info.setText(m.getInfo());

		Element email = new Element("email");
		email.setText(m.getEmail());
		
		Element password = new Element("password");
		password.setText(m.getPassword());
		
		Element mid = new Element("mid");
		mid.setText(String.valueOf(m.getMid()));
		
		root.addContent(mname);
		root.addContent(type);
		root.addContent(addr);
		root.addContent(tel);
		root.addContent(logo);
		root.addContent(info);
		root.addContent(email);
		root.addContent(password);
		root.addContent(mid);

		XMLOutputter out = new XMLOutputter();
		OutputStream fo = new FileOutputStream("merchant.xml");

		Format format = Format.getPrettyFormat();
		format.setEncoding("UTF-8");
		XMLOutputter xmlout = new XMLOutputter(format);
		ByteArrayOutputStream bo = new ByteArrayOutputStream();
		xmlout.output(doc, bo);
		out.output(doc, fo);
		fo.close();
		return bo.toString();
	}

	public static void sendXML(Merchant m) throws JMSException, IOException {
		
		String data = merchantToXML(m);
		Connection con = factory.createConnection();
		con.start();

		Session sen = con.createSession(false, Session.AUTO_ACKNOWLEDGE);
		MessageProducer producer = sen.createProducer(queue);
		TextMessage msgMessage = (TextMessage) sen.createTextMessage(data);
		producer.send(msgMessage);
		producer.close();
		sen.close();
		con.close();
	}

	public static TextMessage recieveXML() throws FileNotFoundException,
			IOException, JMSException {
		Connection con = factory.createConnection();
		con.start();

		Session sen = con.createSession(false, Session.AUTO_ACKNOWLEDGE);
		MessageConsumer consumer = sen.createConsumer(queue);
		TextMessage msg = (TextMessage) consumer.receive();
		consumer.close();

		sen.close();
		con.close();
		return msg;
	}

	public static Merchant XMLToMerchant() throws IllegalAccessException,
			IllegalArgumentException, InvocationTargetException,
			NoSuchMethodException, SecurityException, JDOMException,
			IOException, JMSException {
		SAXBuilder builder = new SAXBuilder();
		TextMessage msg = recieveXML();
		Document doc = builder.build(new ByteArrayInputStream(msg.getText()
				.getBytes()));
		Merchant m = new Merchant();
		Element root = doc.getRootElement();
		List<Element> es = root.getChildren();
		m.setMname(es.get(0).getText());
		m.setType(es.get(1).getText());
		m.setAddress(es.get(2).getText());
		m.setTel(es.get(3).getText());
		m.setLogo(es.get(4).getText());
		m.setInfo(es.get(5).getText());
		m.setEmail(es.get(6).getText());
		m.setPassword(es.get(7).getText());
		m.setMid(Integer.parseInt(es.get(8).getText()));
		return m;
	}

}
